package com.lukk.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lukk.constant.Const;
import com.lukk.pojo.InternalWorker;
import com.lukk.pojo.PotentialWorker;
import com.lukk.service.IPotentialWorkerService;
import com.lukk.util.DateUtils;
import com.lukk.util.jpush.JSMSUtil;
/**
 * 
 * PotentialWorkerController 上午10:21:57
 * 
 * Copyright zhaocj Inc. All rights reserved.
 * Love ME Like Justin Bieber.
 */
@Controller
@RequestMapping("/potentialWorker")
public class PotentialWorkerController {
	private static Logger logger = LogManager.getLogger(PotentialWorkerController.class.getName());
	@Autowired
	private IPotentialWorkerService potentialWorkerService;

	public IPotentialWorkerService getPotentialWorkerService() {
		return potentialWorkerService;
	}

	public void setPotentialWorkerService(IPotentialWorkerService potentialWorkerService) {
		this.potentialWorkerService = potentialWorkerService;
	}

	// 解析视图
	@RequestMapping("/addWorker")
	public String measure(Model model) {
		model.addAttribute("oneMenu", "workerManage");
		model.addAttribute("twoMenu", "addWorker");
		return "/cms/workerManage/addWorker";
	}

	@RequestMapping("/viewWorker")
	public String selectPWorkersByIWorkerID(Model model) {
		model.addAttribute("oneMenu", "workerManage");
		model.addAttribute("twoMenu", "viewWorker");
		return "/cms/workerManage/viewWorker";
	}

	@RequestMapping("/add")
	public @ResponseBody boolean addPotentialWroker(PotentialWorker pWorker, HttpSession session) {
		if (potentialWorkerService.isCodeAlreadyUsed(pWorker)) {
			return false;
		} else {
			InternalWorker internalWorker = (InternalWorker) session.getAttribute(Const.SESSION_USER);
			pWorker.setInternalWorkerID(internalWorker.getId());
			pWorker.setCreateTime(DateUtils.getDate("yyyy-MM-dd HH:mm:ss"));
			potentialWorkerService.insertPotentialWorker(pWorker);
			return true;
		}
	}

	@RequestMapping("/sendMessage")
	public @ResponseBody boolean sendMessageToWorkers(String workers) {
		String[] split = workers.split(",");
		PotentialWorker potentialWorker = new PotentialWorker();
		for (String string : split) {
			potentialWorker.setId(Integer.parseInt(string));
			PotentialWorker bean = potentialWorkerService.selectByPrimaryKey(potentialWorker);
			JSMSUtil.sendPromoteMessageToUser(bean.getPhoneNumber());
		}
		return true;
	}

	// 分页查询
	@RequestMapping("/queryByPage")
	public @ResponseBody Map<String, Object> queryByPage(Integer offset, Integer limit, String sort, String order,
			PotentialWorker potentialWorker, HttpSession session) throws Throwable {
		InternalWorker internalWorker = (InternalWorker) session.getAttribute(Const.SESSION_USER);
		potentialWorker.setInternalWorkerID(internalWorker.getId());
		potentialWorker.getPageUtil().setPaging(true);
		potentialWorker.getPageUtil().setPageOffset(offset);
		potentialWorker.getPageUtil().setOrderByCondition(sort);
		potentialWorker.getPageUtil().setPageSize(limit);
		potentialWorker.getPageUtil().setQueryCondition(order);
		Map<String, Object> rets = new HashMap<String, Object>();
		List<PotentialWorker> partBeans = potentialWorkerService.selectByBean(potentialWorker);
		// 返回对象准备
		potentialWorker.getPageUtil().setPaging(false);
		rets.put("total", potentialWorkerService.selectByBean(potentialWorker).size());
		rets.put("rows", partBeans);
		return rets;
	}

	// 修改一个用户
	@RequestMapping("/modify")
	public @ResponseBody boolean modifyByBean(PotentialWorker potentialWorker) {
		if (potentialWorkerService.isCodeAlreadyUsed(potentialWorker)) {
			return false;
		} else {
			potentialWorkerService.modifyByBean(potentialWorker);
			return true;
		}
	}

	@RequestMapping("/drop")
	public @ResponseBody boolean drops(String drops) throws Exception {
		logger.debug("要删除的用户的主键有：{}" + drops);
		potentialWorkerService.deleteByPrimaryKey(drops);
		return true;
	}

}
